/**
 * 系统菜单
 */
define(['jquery', 'underscore', 'RootView', 'screenfull'],
    function ($, _, RootView) {
        var AppView = RootView.extend({
            el: 'body',
            events: {
                'click                     [data-toggle="menubar"]': 'toggleMenubar',
                'mouseenter         .menu [data-toggle="menulink"]': 'toggleMenulink',
                'mouseleave                                  .menu': 'closeMenulink',
                'click                     [data-toggle="submenu"]': 'toggleSubmenu'
            },
            initialize: function () {
                var that = this;
                that.localStorage = window.localStorage;

                if(!that.localStorage['MENUBAR_STATUS']){
                    if ($(window).width() < 970) {
                        that.localStorage['MENUBAR_STATUS'] = 'min';
                    } else {
                        that.localStorage['MENUBAR_STATUS'] = 'max';
                    }
                }
                this.renderMenubar();
            },
            closeMenulink: function () {
                $('.menu-item').removeClass('open');
            },
            toggleMenulink: function (event) {
                var $target = $(event.target),
                    $targetToggle = $target.closest('[data-toggle="menulink"]'),
                    $menuItem = $targetToggle.closest('.menu-item');

                $('.menu-item').removeClass('open');

                $menuItem.toggleClass('open');
            },


            //# 菜单控制
            //
            toggleMenubar: function () {
                var that = this;
                switch (that.localStorage['MENUBAR_STATUS']) {
                    case 'min':
                        that.localStorage['MENUBAR_STATUS'] = 'max';
                        break;
                    case 'max':
                        that.localStorage['MENUBAR_STATUS'] = 'min';
                        break;
                }

                that.renderMenubar();
            },

            renderMenubar: function () {
                var that = this;
                switch (that.localStorage['MENUBAR_STATUS']) {
                    case 'min':
                        that.$el.addClass('menubar-fold');
                        that
                        break;
                    case 'max':
                        that.$el.removeClass('menubar-fold');
                        break;
                    default:
                        if ($(window).width() < 970) {
                            if (!that.$el.hasClass('menubar-fold')) {
                                that.$el.addClass('menubar-fold');
                            }
                        } else {
                            that.$el.removeClass('menubar-fold');
                        }
                        break;
                }
            },
            /**
             * 二级菜单控制
             */
            toggleSubmenu: function (event) {
                var $target = $(event.target),
                    $menuItem = $target.closest('.menu-item'),
                    $menu = $menuItem.closest('.menu');
                if (!$menuItem.hasClass('open')) {
                    $menu.find('.menu-item').removeClass('open');
                }

                $menuItem.toggleClass('open');
            },
            //# 切换按钮title
            //
            toggleMenuToggleTitle: function () {
                if ($('.maxmenubar').hasClass('hide')) {
                    $('.maxmenubar').closest('a').attr('title', '展开菜单');
                } else {
                    $('.maxmenubar').closest('a').attr('title', '缩起菜单');
                }
            }

        });
        return AppView;
    });
